// 地图  张
var express = require('express');
var router = express.Router();
const db = require('../../db/db');

//map信息获取(围栏、牛牛信息)
router.get('/fence', function (req, res, next) {
    let info = {};
    let error = false;
    let livestockInfo = [];
    db.query("SELECT F_Map FROM nc_fence where F_Id='08dc1f1c-3d93-403b-8ec5-ccb2a095c2db'", function (err, rows, fields) {
        if (err) {
            res.send({ code: 0, msg: '大概是网卡了......' }); // 或者 res.status(500).json({ error: err.message });
            error = true;
        }
        else {
            info.fenceInfo=rows;
        }
    });
    if (!error) {
        db.query("SELECT F_Area,F_Zc FROM nc_pasture WHERE F_Id='08dc1669-e8db-4e50-85c7-95d689b5b185'", function (err, rows, fields) {
            if (err) {
                res.send({ code: 0, msg: '大概是网卡了......' });
                error = true;
            }
            else {
                info.NumInfo=rows;
            }
        })
    }
    if (!error) {
        db.query('select F_Id,F_IMEI,F_Isster from nc_livestock', function (err, rows, fields) {
            if (err) {
                res.send({ code: 0, msg: '大概是网卡了......' }); // 或者 res.status(500).json({ error: err.message });
                error = true;
            }
            else {
                livestockInfo=rows;
            }
        });
    }
    if (!error) {
        db.query("select lng,lat,F_LivestockId from nc_device_eartag", function (err, rows, fields) {
            if (err) {
                res.send({ code: 0, msg: '大概是网卡了......' }); // 或者 res.status(500).json({ error: err.message });
                error = true;
            }
            else {
                info.locationInfo=rows;
            }
        })
    }
    setTimeout(() => {
        if (!error) {
            let newlocationInfo = [];
            for(let i=0;i<info.locationInfo.length;i++){
                let livestockId = info.locationInfo[i].F_LivestockId;
                for(let j=0;j<livestockInfo.length;j++){
                    if(livestockInfo[j].F_Id==livestockId){
                        newlocationInfo.push({
                            lng:info.locationInfo[i].lng,
                            lat:info.locationInfo[i].lat,
                            F_IMEI:livestockInfo[j].F_IMEI,
                            F_Isster:livestockInfo[j].F_Isster
                        });
                        break;
                    }
                }
            }
            info.locationInfo=newlocationInfo;
            res.send({ code: 200, msg: '成功', data: info });
        }
    }, 200);
});













module.exports = router;